package org.swingDemo;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Currency;
import java.util.Locale;

public class NumberFormatDemo {

    public static void displayNumber(Locale locale) {
        Integer quantity = new Integer(123456);
        Double amount = new Double(345987.456);
        NumberFormat numberFormat;
        String quantityOut;
        String amountOut;

        numberFormat = NumberFormat.getNumberInstance(locale);
        quantityOut = numberFormat.format(quantity);
        amountOut = numberFormat.format(amount);
        System.out.println(quantityOut + "   " + locale);
        System.out.println(amountOut + "   " + locale);
    }

    public static void displayCurrency(Locale locale) {
        Double currencyAmount = new Double(9876543.21);
        Currency currentCurrency;
        try {
            currentCurrency = Currency.getInstance(locale);
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
            return;
        }
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(locale);
        System.out.println(locale.getDisplayName() + ", " + currentCurrency.getDisplayName() + ", "
                + currencyFormat.format(currencyAmount));
    }

    public static void displayPercent(Locale locale) {
        Double percent = new Double(0.75);
        NumberFormat percentFormat;
        String percentOut;

        percentFormat = NumberFormat.getPercentInstance(locale);
        percentOut = percentFormat.format(percent);
        System.out.println(percentOut + "   " + locale);
    }

    public static void main(String[] args) {
        ArrayList<Locale> locales = new ArrayList<>();
        locales.add(0, new Locale.Builder().setLanguage("zh").setRegion("CN").build());
        locales.add(1, new Locale.Builder().setLanguage("fr").setRegion("FR").build());
        locales.add(2, new Locale.Builder().setLanguage("de").setRegion("DE").build());
        locales.add(3, new Locale.Builder().setLanguage("en").setRegion("US").build());

        for (Locale locale : locales) {
            displayNumber(locale);
        }

        for (Locale locale : locales) {
            displayCurrency(locale);
        }

        for (Locale locale : locales) {
            displayPercent(locale);
        }
    }
}
